<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #container {
        width: 500px;
        height: 500px;
        position: relative;
        overflow: hidden;
      }

      #preview {
        width: 100%;
        height: 100%;
      }

      #image {
        width: 100%;
        height: 100%;
        transition: transform 0.2s;
        transform-origin: 0 0;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div id="preview">
        <img src="./test.png" id="image" />
      </div>
    </div>
  </body>
  <script>
    const preview = document.querySelector("#preview");
    const container = document.getElementById("container");
    const image = document.getElementById("image");

    let factor = {
      scale: 1,
    };

    const imageParams = {
      naturalHeight: image.naturalHeight,
      naturalWidth: image.naturalWidth,
    };

    preview.addEventListener("wheel", (event) => {
      event.preventDefault();

      const scaleDelta = event.deltaY * -1; // 可根据实际需求调整缩放速度
      const offsetX = event.offsetX / preview.clientWidth;
      const offsetY = event.offsetY / preview.clientHeight;

      const scaleVal = scaleDelta > 0 ? 0.2 : -0.2;
      factor.scale += scaleVal;
      if (factor.scale < 1) {
        factor = {
          scale: 1,
        };
      }

      preview.style.transformOrigin = `${offsetX * 100}% ${offsetY * 100}%`;
      preview.style.transform = `scale(${factor.scale})`;
    });

    preview.onclick = function (e) {
      const needWidth =
        e.offsetX / (container.clientWidth / image.naturalWidth);
      const needHeight =
        e.offsetY / (container.clientHeight / image.naturalHeight);

      const left = needWidth * (container.clientWidth / image.naturalWidth);
      const top = needHeight * (container.clientHeight / image.naturalHeight);

      const span = document.createElement("span");
      span.style.width = "10px";
      span.style.height = "10px";
      span.style.borderRadius = "5px";
      span.style.background = "red";
      span.style.position = "absolute";
      span.style.left = left + "px";
      span.style.top = top + "px";
      span.style.transform = "translate(-50%, -50%)";

      span.onwheel = function (e) {
        e.stopPropagation();
      };

      preview.appendChild(span);
    };
  </script>
</html>
